package cn.wapache.i18n;

public class I18nException extends RuntimeException {

	private static final long serialVersionUID = -3292754449086732926L;

	String code;

	public I18nException(Throwable cause) {
		super(cause);
		if(cause instanceof I18nException ){
			this.code = ((I18nException)cause).code;
		}
	}
	
//	public I18nException(String code, String value) {
//		super(I18n.getDescription(code, value));
//		this.code = code;
//	}
	
	public I18nException(String code, Object...values) {
		this(code, I18n.getDescription(code, values));
	}
	
	public I18nException(String code, String message, Object...values) {
		super(values.length==0 ? message : String.format(message, values));
		this.code = code;
	}
	
	public I18nException(Throwable cause, String code, Object...values) {
		super(I18n.getDescription(code, values), cause);
		this.code = code;
	}

	public I18nException(Throwable cause, String code, String message, Object...values) {
		super(String.format(message, values), cause);
		this.code = code;
	}
	
	public String getCode() {
		return code;
	}
	
	public String getDescription(Object...values) {
		return code==null || code.isEmpty() ? "" : I18n.getDescription(code, values);
	}

	@Override
	public String toString() {
		return "Error [code=" + code + ", message=" + getMessage() + "]";
	}
	
}
